package com.itheima.web.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.QuestionItem;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.QuestionItemService;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/store/questionItem/*")
public class QuestionItemServlet extends BaseServlet {
    private QuestionItemService questionItemService = BeanFactory.getBean("questionItemService",QuestionItemService.class);
    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            String questionId = request.getParameter("questionId");
            List<QuestionItem> list = questionItemService.findAll(questionId);
            return new Result(true,MessageConstant.QUERY_QUESTIONITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_QUESTIONITEM_FAIL);
        }
    }

    private Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            String id = request.getParameter("id");
            questionItemService.delete(id);
            return new Result(true,MessageConstant.DELETE_QUESTIONITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_QUESTIONITEM_FAIL);
        }
    }

    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            QuestionItem questionItem = BeanUtil.fillBeanFromJson(request, QuestionItem.class);
            
            questionItemService.update(questionItem);
            return new Result(true,MessageConstant.EDIT_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_COMPANY_FAIL);
        }
    }

    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            String id = request.getParameter("id");
            
            QuestionItem questionItem = questionItemService.findById(id);
            return new Result(true,MessageConstant.QUERY_COMPANY_SUCCESS,questionItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_COMPANY_FAIL);
        }
    }

    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            QuestionItem questionItem = BeanUtil.fillBeanFromJson(request, QuestionItem.class);
            
            questionItemService.save(questionItem);
            return new Result(true,MessageConstant.SAVE_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SAVE_COMPANY_FAIL);
        }
    }

    
}
